﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;

namespace Codes {
    public class ContentGroup {
        /// <summary>
        /// 添加内容分组
        /// </summary>
        /// <param name="title">分组名</param>
        /// <param name="pyname">拼音</param>
        /// <param name="state">状态</param>
        /// <param name="order">排序</param>
        /// <param name="contentType">内容类型</param>
        /// <returns></returns>
        public static bool add(string title, string pyname, string state="1", string order="0", string contentType="") {
            string sql="insert into ContentGroup([title],[pyname],[state],[order],[contentType])";
            sql+=" values(@title,@pyname,@state,@order,@contentType)";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@title", title),
                new OleDbParameter("@pyname", pyname),
                new OleDbParameter("@state", state),
                new OleDbParameter("@order", order),
                new OleDbParameter("@contentType", contentType)
            })>0;
        }
        public static bool update(string ID, string title, string pyname, string state, string order, string contentType) {
            string sql="update ContentGroup set [title]=@title,[pyname]=@pyname,[state]=@state,[order]=@order,[contentType]=@contentType where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@title", title),
                new OleDbParameter("@pyname", pyname),
                new OleDbParameter("@state", state),
                new OleDbParameter("@order", order),
                new OleDbParameter("@contentType", contentType),
                new OleDbParameter("@ID", ID)
            })>0;
        }
        public static bool changeOrder(int Order, int ID) {
            string sql="update ContentGroup set [order]=@Order where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql, new OleDbParameter[]{
                new OleDbParameter("@Order", Order),
                new OleDbParameter("@ID", ID)
            })>0;
        }
        public static bool deleteMass(string idList) {
            string sql="delete from ContentGroup where ID in("+idList+")";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Content, sql)>0;
        }

        public static int getListCount() {
            string sql ="select count(1) from ContentGroup";
            return (int)Codes.AccessHelper.ExecuteScalar(connectionString.Content, sql);
        }
        public static System.Data.DataTable getList(int pageindex, int pagesize) {
            int notTop=((pageindex-1)*pagesize);
            string sql=string.Empty;
            if(notTop==0) {
                sql="select top "+pagesize+" * from ContentGroup order by [order] asc";
            } else {
                sql ="select top "+pagesize+" * from ContentGroup where id not in(select top "+((pageindex-1)*pagesize)+" id from ContentGroup) order by [order] asc";
            }
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }

        public static System.Data.DataTable getInfo(string id) {
            string sql="select * from ContentGroup where id="+id;
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Content, sql).Tables[0];
        }
    }
}